<template>
  <div class="home-page">
    <div class="home-page__userinfo">
      <span class="logo">用户管理系统</span>
      <img :src="userInfo.avatar"/>
      <span class="nickname">{{ userInfo.nickname }}</span>
      <button class="logout-btn" @click="handleLogout">退出登录</button>
    </div>
    <table class="table-c" border="0" cellpadding="0" cellspacing="0" align="center">
      <tr>
        <th>#</th>
        <th>用户名</th>
        <th>昵称</th>
        <th>创建时间</th>
        <th>操作</th>
      </tr>
      <tr v-for="item in userList" :key="item.id">
        <td>
          <img :src="item.avatar" alt="" style="width: 40px;height: 40px;border-radius: 50%">
        </td>
        <td>{{ item.username }}</td>
        <td>{{ item.nickname }}</td>
        <td>{{ $dayjs(item.create_time).format('YYYY-MM-DD HH:mm:ss') }}</td>
        <td>
          <button>查看</button>
        </td>
      </tr>
    </table>
  </div>
</template>

<script>
export default {
  name: 'HomePage',
  data() {
    return {
      userInfo: {},
      userList: [],
      total: 0
    }
  },
  mounted() {
    this.getUserInfo()
    this.getUserList()
  },
  methods: {
    httpClient(methods, url, params, data) {
      const {basePath, $axios} = this;
      const token = localStorage.getItem('token')
      return new Promise((resolve, reject) => {
        $axios({
          methods,
          url: `${basePath}${url}`,
          headers: {'authorization': token},
          params,
          data
        }).then(res => {
          if (res.status === 200 && res.data.reqCode === 0) {
            resolve(res.data.reqData)
          } else {
            localStorage.clear();
            location.reload();
          }
        }).catch(err => {
          reject(err);
          localStorage.clear();
          location.reload();
        })
      })
    },
    async getUserInfo() {
      const token = localStorage.getItem('token')
      this.userInfo = await this.httpClient(
          'GET',
          '/admin/getUserInfo',
          {
            token
          }
      )
    },
    async getUserList() {
      const {count, rows} = await this.httpClient(
          'GET',
          '/admin/getUserList'
      )
      this.total = count;
      this.userList = rows;
    },
    handleLogout() {
      localStorage.clear();
      location.reload()
    }
  }
}
</script>

<style scoped>
.home-page {
}

.home-page__userinfo {
  padding: 0 20px;
  height: 80px;
  box-shadow: 0 0 10px #ddd;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  position: relative;
}

.home-page__userinfo .logo {
  position: absolute;
  left: 20px;
  font-weight: 800;
}

.home-page__userinfo img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.home-page__userinfo .nickname {
  display: block;
  font-size: 12px;
  margin: 0 10px;
}

.home-page__userinfo .logout-btn {
  width: 50px;
  height: 50px;
  background: none;
  border: 1px solid #dddddd;
  font-size: 12px;
  padding: 10px;
  box-sizing: border-box;
  border-radius: 50%;
  transform: scale(0.85);
}

.home-page__userinfo .logout-btn:hover {
  background-color: #E6A23C;
  color: white;
  cursor: pointer;
}


.table-c {
  width: 60%;
  margin: 40px auto;
}

.home-page table {
  border-right: 1px solid #ddd;
  border-bottom: 1px solid #ddd
}

.home-page table th {
  border-left: 1px solid #ddd;
  border-top: 1px solid #ddd;
  padding: 10px;
  font-size: 14px;
  color: #888;
}

.home-page table td {
  border-left: 1px solid #ddd;
  border-top: 1px solid #ddd;
  text-align: center;
  font-size: 12px;
}

.home-page table td button {
  display: inline-block;
  line-height: 1;
  white-space: nowrap;
  background: #fff;
  border: 1px solid #dcdfe6;
  color: #606266;
  text-align: center;
  box-sizing: border-box;
  outline: none;
  margin: 0;
  transition: .1s;
  font-weight: 500;
  font-size: 12px;
  border-radius: 4px;
  padding: 7px 15px;
}
</style>
